<template>
    <div v-if="store.auth.role!='学生'">
    <el-button class="mt-4" style="" @click="dialogVisible=true" type="success">新增</el-button>
    </div>
    <div v-if="store.auth.role!='学生'">
        <el-input placeholder="按预警号搜索" v-model="warningidd" type="text" style="width: 140px"/>
        <el-button type="primary" @click="searchWarBywarnid">搜索</el-button>
        <el-input placeholder="按学号搜索" v-model="idd" type="text" style="width: 140px"/>
        <el-button type="primary" @click="searchWarByid">搜索</el-button>
    </div>
    <el-table :data="datas.slice((pageNum-1)*pageSize,pageNum*pageSize)" style="width: 100%" max-height="250" border :row-style="rowStyle">
        <el-table-column fixed label="序号" width="100">
            <template #default="scopes">
                {{ scopes.$index+1 }}
            </template>
        </el-table-column>
        <el-table-column fixed prop="stuname" label="学生姓名" width="100" />
        <el-table-column prop="stuid" label="学号" width="100" />
        <el-table-column prop="stumajor" label="专业" width="100" />
        <el-table-column prop="stuclassi" label="班级" width="100" />
        <el-table-column prop="warningtype" label="预警类型" width="100" />
        <el-table-column prop="warningdetail" label="预警详情" width="350" />
        <el-table-column prop="term" label="学期" width="150" />
        <el-table-column fixed="right" label="操作" width="150">
            <template #default="scope">
                <div v-if="store.auth.role!='学生'">
                <el-button
                        link
                        type="primary"
                        size="small"
                        @click.prevent="deleteRow(scope.row.warningid)"
                >
                    删除
                </el-button>
                <el-button
                        link
                        type="primary"
                        size="small"
                        @click.prevent="getWarByid(scope.row.warningid)"
                >
                    修改
                </el-button>
                    <el-button
                            link
                            type="primary"
                            size="small"
                            @click.prevent="getWarByids(scope.row.warningid)"
                            v-if="scope.row.status===0"
                    >
                        审核
                    </el-button>
                </div>
                <div v-if="store.auth.role=='学生'">无权限操作</div>
            </template>
        </el-table-column>
    </el-table>
    <div class="pagination">
        <el-pagination
                background
                layout="sizes,prev,pager,next,jumper,->,total"
                :page-size.sync="pageSize"
                :current-page.sync="pageNum"
                :page-sizes="[5,8,10,15]"
                :total="totals"
                :hide-on-single-page="true"
                @current-change="getList"
                @size-change="getList1"
        />
    </div>
    <el-dialog title="填写信息" v-model="dialogVisible">
        <el-form :model="stu">
            <el-input placeholder="预警编号" v-model="stu.warningid" type="text" />
            <el-input placeholder="姓名" v-model="stu.stuname" type="text" />
            <el-input placeholder="学号" v-model="stu.stuid" type="text" />
            <el-input placeholder="专业" v-model="stu.stumajor" type="text" />
            <el-input placeholder="班级" v-model="stu.stuclassi" type="text" />
            <el-select v-model="stu.warningtype" placeholder="请选择预警类型" clearable>
                <el-option label="轻度" value="1" />
                <el-option label="中度" value="2" />
                <el-option label="重度" value="3" />
            </el-select>
            <el-input placeholder="预警详情" v-model="stu.warningdetail" type="textarea" />
            <el-input placeholder="预警学期" v-model="stu.term" type="textarea" />
            <el-button type="warning" @click="dialogVisible=false">取消</el-button>
            <el-button type="primary" @click="send">确认</el-button>
        </el-form>
    </el-dialog>
    <el-dialog title="修改信息" v-model="dialogVisibles">
        <el-form :model="stus">
            <el-input placeholder="预警编号" v-model="stus.warningid" type="text" disabled/>
            <el-input placeholder="姓名" v-model="stus.stuname" type="text" disabled/>
            <el-input placeholder="学号" v-model="stus.stuid" type="text" disabled/>
            <el-input placeholder="专业" v-model="stus.stumajor" type="text" disabled/>
            <el-input placeholder="班级" v-model="stus.stuclassi" type="text" disabled/>
            <el-select v-model="stus.warningtype" placeholder="请选择预警类型" clearable>
                <el-option label="轻度" value="1" />
                <el-option label="中度" value="2" />
                <el-option label="重度" value="3" />
            </el-select>
            <el-input placeholder="预警详情" v-model="stus.warningdetail" type="textarea" />
            <el-button type="warning" @click="dialogVisibles=false">取消</el-button>
            <el-button type="primary" @click="adds">确认</el-button>
        </el-form>
    </el-dialog>
    <el-dialog title="审核信息" v-model="dialogVisibless">
        <el-form :model="stuss">
            <el-input placeholder="预警编号" v-model="stuss.warningid" type="text" disabled/>
            <el-input placeholder="姓名" v-model="stuss.stuname" type="text" disabled/>
            <el-input placeholder="学号" v-model="stuss.stuid" type="text" disabled/>
            <el-input placeholder="专业" v-model="stuss.stumajor" type="text" disabled/>
            <el-input placeholder="班级" v-model="stuss.stuclassi" type="text" disabled/>
            <el-select v-model="stuss.warningtype" placeholder="请选择预警类型" clearable>
                <el-option label="轻度" value="1" />
                <el-option label="中度" value="2" />
                <el-option label="重度" value="3" />
            </el-select>
            <el-input placeholder="预警详情" v-model="stuss.warningdetail" type="textarea" />
            <el-button type="warning" @click="dialogVisibless=false">取消</el-button>
            <el-button type="primary" @click="check">通过</el-button>
        </el-form>
    </el-dialog>

</template>

<script setup>
import { ref,onMounted } from 'vue'
import {ElMessage} from 'element-plus'
import { get,post } from '@/utils/request.js'
import { reactive } from 'vue'
import moment from 'moment'
import {userStore} from '@/stores/counter.js'

const store=userStore()
const datas=ref([])
const dialogVisible=ref(false)
const dialogVisibles=ref(false)
const dialogVisibless=ref(false)
const num=ref('')
const warningidd=ref('')
const idd=ref('')
const pan=ref('')
const types=ref('')
const totals=ref('')
const pageNum=ref(1)
const pageSize=ref(5)

const stu = reactive({
warningid:'',
stuid:'',
stuname:'',
stumajor:'',
stuclassi:'',
warningtype:'',
warningdetail:'',
term:'',
status:''
})

const stus = reactive({
warningid:'',
stuid:'',
stuname:'',
stumajor:'',
stuclassi:'',
warningtype:'',
warningdetail:'',
status:''
})

const stuss = reactive({
warningid:'',
stuid:'',
stuname:'',
stumajor:'',
stuclassi:'',
warningtype:'',
warningdetail:'',
term:'',
status:''
})

const rowStyle=({row,index})=>{
if(row.warningtype == '重度' )
return { 'color': 'red','font-weight': 'bolder'}
else return ''
}

    get('/getallwarning',(message)=>{
    totals.value=message.length
    if(store.auth.role!='学生'){
    let ss=[]
    let sss=[]
    for(let i=0;i<message.length;i++){
if(message[i].warningtype=='1')message[i].warningtype='轻度'
else if(message[i].warningtype=='2')message[i].warningtype='中度'
else message[i].warningtype='重度'
}
    ss=message.filter(item=>item.flag===2)
    sss=ss.map(item=>({
    ...item,
     createtime: moment(item.createtime).format('YYYY-MM-DD'),
     utime: moment(item.utime).format('YYYY-MM-DD'),
    }))
    datas.value = sss
    }else {
    let aaa=[]
    let bbb=[]
        for(let i=0;i<message.length;i++){
if(message[i].warningtype=='1')message[i].warningtype='轻度'
else if(message[i].warningtype=='2')message[i].warningtype='中度'
else message[i].warningtype='重度'
}
     aaa = message.filter(item=>item.stuname===store.auth.user)
     bbb=aaa.map(item=>({
    ...item,
     createtime: moment(item.createtime).format('YYYY-MM-DD'),
     utime: moment(item.utime).format('YYYY-MM-DD'),
    }))
    datas.value = bbb.filter(item => {
    if (item.flag === 2) {
        return true; // 保留满足条件的项
    } else {
        return false; // 删除不满足条件的项
    }
})
totals.value=datas.value.length
    }
    },(message)=>{
    ElMessage.warning(message)
    })

const check=()=>{
post('/checkwarning',{
warningid:stuss.warningid,
warningtype:stuss.warningtype,
warningdetail:stuss.warningdetail
},(message)=>{
ElMessage.success(message)
dialogVisibless.value = false
  get('/getallwarning',(message)=>{
    totals.value=message.length
    if(store.auth.role!='学生'){
      let ss=[]
      let sss=[]
      for(let i=0;i<message.length;i++){
        if(message[i].warningtype=='1')message[i].warningtype='轻度'
        else if(message[i].warningtype=='2')message[i].warningtype='中度'
        else message[i].warningtype='重度'
      }
      ss=message.filter(item=>item.flag===2)
      sss=ss.map(item=>({
        ...item,
        createtime: moment(item.createtime).format('YYYY-MM-DD'),
        utime: moment(item.utime).format('YYYY-MM-DD'),
      }))
      datas.value = sss
    }else {
      let aaa=[]
      let bbb=[]
      for(let i=0;i<message.length;i++){
        if(message[i].warningtype=='1')message[i].warningtype='轻度'
        else if(message[i].warningtype=='2')message[i].warningtype='中度'
        else message[i].warningtype='重度'
      }
      aaa = message.filter(item=>item.stuname===store.auth.user)
      bbb=aaa.map(item=>({
        ...item,
        createtime: moment(item.createtime).format('YYYY-MM-DD'),
        utime: moment(item.utime).format('YYYY-MM-DD'),
      }))
      datas.value = bbb.filter(item => {
        if (item.flag === 2) {
          return true; // 保留满足条件的项
        } else {
          return false; // 删除不满足条件的项
        }
      })
      totals.value=datas.value.length
    }
  },(message)=>{
    ElMessage.warning(message)
  })
},(message)=>{
ElMessage.warning(message)
dialogVisibless.value = false
})
}

const deleteRow = (index) => {
post('/deletewarning',{
warningid:datas.value[index].warningid
},(message)=>{
ElMessage.success(message)
num.value=index
datas.value.splice(index, 1)
},(message)=>{
ElMessage.warning(message)
})
}

const send=()=>{
post('/addwarninginfo',{
stuid:stu.stuid,
stuname:stu.stuname,
stumajor:stu.stumajor,
stucourse:stu.stucourse,
stuclassi:stu.stuclassi,
warningid:stu.warningid,
warningtype:stu.warningtype,
detail:stu.warningdetail
},(message)=>{
ElMessage.success(message)
dialogVisible.value = false
datas.value.splice(num,1,stu)
},(message)=>{
ElMessage.warning(message)
dialogVisible.value = false
})
}

const getWarByid=(index)=>{
post('/getwarningbyid',{
warningid:index
},(message)=>{
dialogVisibles.value=true
stus.warningid=message.warningid
stus.stuid=message.stuid
stus.stuname=message.stuname
stus.stumajor=message.stumajor
stus.stuclassi=message.stuclassi
stus.warningdetail=message.warningdetail
if(message.warningtype=='1'){stus.warningtype='轻度'
}
else if(message.warningtype=='2'){stus.warningtype='中度'
}
else {stus.warningtype='重度'
}
},(message)=>{
ElMessage.warning(message)
})
}

const getWarByids=(index)=>{
post('/getwarningbyid',{
warningid:index
},(message)=>{
dialogVisibless.value=true
stuss.warningid=message.warningid
stuss.stuid=message.stuid
stuss.stuname=message.stuname
stuss.stumajor=message.stumajor
stuss.stuclassi=message.stuclassi
stuss.warningdetail=message.warningdetail
stuss.warningtype=message.warningtype
},(message)=>{
ElMessage.warning(message)
})
}

const searchWarBywarnid=()=>{
post('/getwarningbyid',{
warningid:warningidd.value
},(message)=>{
datas.value=[]
if(message.warningtype=='1')pan.value='轻度'
else if(message.warningtype=='2')pan.value='中度'
else pan.value='重度'
datas.value.push({
  warningid: message.warningid,
  stuid: message.stuid,
  stuname: message.stuname,
  stumajor: message.stumajor,
  stuclassi: message.stuclassi,
  warningdetail: message.warningdetail,
  warningtype: pan.value,
  createtime: moment(message.createtime).format('YYYY-MM-DD'),
  utime: moment(message.utime).format('YYYY-MM-DD'),
  term:message.term,
  status: message.status
});
warningidd.value=''
  totals.value=datas.value.length
},(message)=>{
warningidd.value=''
ElMessage.warning(message)
})
}

const searchWarByid=()=>{
post('/searchwarbyid',{
id:idd.value
},(message)=>{
datas.value=[]
  let ss;
  ss=message.filter(item=>item.flag===2)
for(let i=0;i<ss.length;i++){
if(ss[i].warningtype=='1')ss[i].warningtype='轻度'
else if(ss[i].warningtype=='2')ss[i].warningtype='中度'
else ss[i].warningtype='重度'
datas.value.push({
  warningid: ss[i].warningid,
  stuid: ss[i].stuid,
  stuname: ss[i].stuname,
  stumajor: ss[i].stumajor,
  stuclassi: ss[i].stuclassi,
  warningdetail: ss[i].warningdetail,
  warningtype: ss[i].warningtype,
  createtime: moment(ss[i].createtime).format('YYYY-MM-DD'),
  utime: moment(ss[i].utime).format('YYYY-MM-DD'),
  term:ss[i].term,
  status: ss[i].status
});
}
idd.value=''
  totals.value=datas.value.length
},(message)=>{
idd.value=''
ElMessage.warning(message)
})
}

const adds=()=>{
post('/updatewarning',{
warningid:stus.warningid,
warningtype:stus.warningtype,
warningdetail:stus.warningdetail
},(message)=>{
ElMessage.success("update success")
dialogVisibles.value = false
datas.value.splice(num,1)
datas.value.push({
  warningid: stus.warningid,
  stuid: stus.stuid,
  stuname: stus.stuname,
  stumajor: stus.stumajor,
  stuclassi: stus.stuclassi,
  warningdetail: stus.warningdetail,
  warningtype: stus.warningtype,
  createtime: moment(message.createtime).format('YYYY-MM-DD'),
  utime: moment(message.updatetime).format('YYYY-MM-DD')
});
},(message)=>{
ElMessage.warning("error update")
dialogVisibles.value = false
})
}

const getList=(pageNums)=>{
pageNum.value=pageNums
}

const getList1=(pageSizes)=>{
pageSize.value=pageSizes
}

</script>
<style scoped>
.red-bg {
  background-color: red;
}
</style>
